package com.hnyfkj.jyindustry.common.utils;

import java.util.ArrayList;
import java.util.List;

import com.hnyfkj.jyindustry.biz.sysmenu.dto.SysMenuTreeDto;
import com.hnyfkj.jyindustry.biz.sysmenu.dto.SysMenuVoDto;

/**
 * @author zjq
 *
 * @Description
 *
 * @date 2020年12月10日
 */
public class TreeUtil {
	public static List<SysMenuVoDto> toTree(List<SysMenuVoDto> treeList, Long pid) {
		List<SysMenuVoDto> retList = new ArrayList<SysMenuVoDto>();
		for (SysMenuVoDto parent : treeList) {
			if (pid.equals(parent.getPid())) {
				retList.add(findChildren(parent, treeList));
			}
		}
		return retList;
	}

	private static SysMenuVoDto findChildren(SysMenuVoDto parent, List<SysMenuVoDto> treeList) {
		for (SysMenuVoDto child : treeList) {
			if (parent.getId().equals(child.getPid())) {
				if (parent.getChild() == null) {
					parent.setChild(new ArrayList<>());
				}
				parent.getChild().add(findChildren(child, treeList));
			}
		}
		return parent;
	}
	
	public static List<SysMenuTreeDto> toMenuTree(List<SysMenuTreeDto> treeList, Long pid) {
		List<SysMenuTreeDto> retList = new ArrayList<SysMenuTreeDto>();
		for (SysMenuTreeDto parent : treeList) {
			if (pid.equals(parent.getPid())) {
				retList.add(findMenuChildren(parent, treeList));
			}
		}
		return retList;
	}

	private static SysMenuTreeDto findMenuChildren(SysMenuTreeDto parent, List<SysMenuTreeDto> treeList) {
		for (SysMenuTreeDto child : treeList) {
			if (parent.getId().equals(child.getPid())) {
				if (parent.getChildren() == null) {
					parent.setChildren(new ArrayList<>());
				}
				parent.getChildren().add(findMenuChildren(child, treeList));
			}
		}
		return parent;
	}
}
